package example.grid.core;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import com.alicloud.openservices.tablestore.TableStoreWriter;
import com.alicloud.openservices.tablestore.model.Column;
import com.alicloud.openservices.tablestore.model.ColumnValue;
import com.alicloud.openservices.tablestore.model.PrimaryKey;
import com.alicloud.openservices.tablestore.model.PrimaryKeyBuilder;
import com.alicloud.openservices.tablestore.model.PrimaryKeyValue;
import com.alicloud.openservices.tablestore.model.RowUpdateChange;
import com.aliyun.tablestore.grid.GridDataWriter;
import com.aliyun.tablestore.grid.consts.Constants;
import com.aliyun.tablestore.grid.model.GridDataSetMeta;
import com.aliyun.tablestore.grid.model.StoreOptions.StoreType;
import com.aliyun.tablestore.grid.model.grid.Grid2D;
import com.aliyun.tablestore.grid.model.grid.Plane;
import com.aliyun.tablestore.grid.utils.BlockUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import ucar.ma2.InvalidRangeException;

public class MyTableStoreDataWriter implements GridDataWriter {
    private String tableName;
    private GridDataSetMeta meta;
    private TableStoreWriter writer;

    public MyTableStoreDataWriter(TableStoreWriter writer, String tableName, GridDataSetMeta dataSetMeta) {
        this.writer = writer;
        this.tableName = tableName;
        this.meta = dataSetMeta;
    }

    private void checkDataSize(String variable, String t, int z, Grid2D grid2D) {
        if (!this.meta.getVariables().contains(variable)) {
            throw new IllegalArgumentException("The data set dose not include this variable: " + variable);
//        } else if (t >= this.meta.gettSize()) {
//            throw new IllegalArgumentException("t must be in range: [0, " + this.meta.gettSize() + ")");
        }
        else if (z >= this.meta.getzSize()) {
            throw new IllegalArgumentException("z must be in range: [0, " + this.meta.getzSize() + ")");
        } else {
            Plane plane = new Plane(grid2D.getOrigin(), grid2D.getShape());
            if (plane.getxRange().getStart() == 0 && plane.getyRange().getStart() == 0) {
                if (plane.getxRange().getSize() != this.meta.getxSize()) {
                    throw new IllegalArgumentException("xSize in grid2D must be equal to gridDataSetMeta's xSize");
                } else if (plane.getyRange().getSize() != this.meta.getySize()) {
                    throw new IllegalArgumentException("ySize in grid2D must be equal to gridDataSetMeta's ySize");
                }
            } else {
                throw new IllegalArgumentException("xStart and yStart in grid2D must be 0");
            }
        }
    }

    private List<Column> splitDataToColumns(Grid2D grid2D) throws InvalidRangeException {
        List<Column> columns = new ArrayList();
        List<Grid2D> blocks = BlockUtil.splitGrid2DToBlocks(grid2D, this.meta.getStoreOptions().getxSplitCount(), this.meta.getStoreOptions().getySplitCount());
        Iterator var4 = blocks.iterator();

        while(var4.hasNext()) {
            Grid2D block = (Grid2D)var4.next();
            columns.add(new Column(String.format(Constants.DATA_BLOCK_COL_NAME_FORMAT, block.getPlane().getxRange().getStart(), block.getPlane().getyRange().getStart()), ColumnValue.fromBinary(block.getDataAsByteArray())));
        }

        return columns;
    }

    private void writeToTableStore(String variable, String t, int z, List<Column> columns) {
        if (columns.size() == 0) {
            throw new IllegalArgumentException("columns are empty");
        } else {
            PrimaryKeyBuilder builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
            builder.addPrimaryKeyColumn(Constants.GRID_DATA_SET_ID_PK_NAME, PrimaryKeyValue.fromString(this.meta.getGridDataSetId()));
            builder.addPrimaryKeyColumn(Constants.VARIABLE_PK_NAME, PrimaryKeyValue.fromString(variable));
            builder.addPrimaryKeyColumn(Constants.T_PK_NAME, PrimaryKeyValue.fromString(t));
            builder.addPrimaryKeyColumn(Constants.Z_PK_NAME, PrimaryKeyValue.fromLong((long)z));
            PrimaryKey pk = builder.build();
            RowUpdateChange rowUpdateChange = new RowUpdateChange(this.tableName, pk);
            int currentSize = 0;

            for(int i = 0; i < columns.size(); ++i) {
                if (currentSize != 0 && currentSize + ((Column)columns.get(i)).getDataSize() > Constants.MAX_REQUEST_SIZE) {
                    this.writer.addRowChange(rowUpdateChange);
                    rowUpdateChange = new RowUpdateChange(this.tableName, pk);
                    currentSize = 0;
                }

                rowUpdateChange.put((Column)columns.get(i));
                currentSize += ((Column)columns.get(i)).getDataSize();
            }

            this.writer.addRowChange(rowUpdateChange);
            this.writer.flush();
        }
    }

    public void writeGrid2D(String variable, int t, int z, Grid2D grid2D) throws Exception {
        this.checkDataSize(variable, "", z, grid2D);
        if (this.meta.getStoreOptions().getStoreType().equals(StoreType.SLICE)) {
            List<Column> columns = this.splitDataToColumns(grid2D);
//            this.writeToTableStore(variable, t, z, columns);
        } else {
            throw new IllegalArgumentException("unsupported store type");
        }
    }
    public void writeGrid2D(String variable, String t, int z, Grid2D grid2D) throws Exception {
        this.checkDataSize(variable, t, z, grid2D);
        if (this.meta.getStoreOptions().getStoreType().equals(StoreType.SLICE)) {
            List<Column> columns = this.splitDataToColumns(grid2D);
            this.writeToTableStore(variable, t, z, columns);
        } else {
            throw new IllegalArgumentException("unsupported store type");
        }
    }
}

